package com.example.shop_2208.login

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.shop_2208.entity.ParamLogin
import com.example.shop_2208.entity.UserEntityResult
import com.example.shop_2208.mvvm.BaseViewModel
import kotlinx.coroutines.launch

/**
 * 登录viewModel
 */
class LoginViewModel:BaseViewModel() {

    val loginSuccess = MutableLiveData<UserEntityResult>()
    val loginFail = MutableLiveData<String>()

    fun login(username:String,password:String){
        //viewmodel作用域   用来调用挂起函数
        viewModelScope.launch {
            //try-catch  收到添加报错的可能，用来接收请求失败的情况
            try {
                val param = ParamLogin(username,password)
               /* val result = apiServer.login(param)
                loginSuccess.value = result*/
                loginSuccess.value = apiServer.login(param)
            }catch (e:Exception){
                //调用失败 ---没有网的时候
                loginFail.value = e.message
            }
        }
    }
}